package main

import (
	"fmt"
	"net/http"
	"os"
)

type fileInfo struct {
	Filename string `json:"filename,attr"`
}

func startWebService() {
	defer myRecover()

	h := http.NewServeMux()
	// 测试接口/hello
	h.HandleFunc("/hello", helloWorldHandler)
	// 基于服务器本地文件进行扫描的接口/serverfile
	h.HandleFunc("/serverfile", serverfileHandler)
	// 基于附带的二进制流进行扫描的接口/binary
	h.HandleFunc("/binary", binaryHandler)

	err := http.ListenAndServe(":12345", h)
	if err != nil {
		fmt.Println("Listen port failed: " + err.Error())
		os.Exit(-1)
	}
}

func helloWorldHandler(writer http.ResponseWriter, request *http.Request) {
	msg := request.FormValue("msg")
	writer.Write([]byte("Got message: " + msg))
}

func serverfileHandler(writer http.ResponseWriter, request *http.Request) {
	var obj fileInfo
	ParseJson(request, &obj)
	onefile := obj.Filename
	scanRes := scanOneFile(onefile)
	writer.Write([]byte(scanRes))
}

func binaryHandler(writer http.ResponseWriter, request *http.Request) {
	msg := request.FormValue("msg")
	writer.Write([]byte("Got message: " + msg))
}
